<?php
error_reporting(E_ALL);
ini_set('display_errors','On');
include_once $_SERVER['DOCUMENT_ROOT']."/math-videos/configure.inc.php";
//include_once $site_path."/header.php";

$username="ima";
$password= "ima2006";
$assetID=208;
/****************start getting data********************
//$ch = curl_init("http://www.mathinstitutes.org/math-videos/metadata/v1/get/asset/".$assetID);    
//$ch = curl_init("http://www.mathinstitutes.org/math-videos/metadata/v1/get/asset/");  
 

$curl = curl_init();

$submit_url = "http://www.mathinstitutes.org/math-videos/metadata/v1/get/asset/";
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ;
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
//curl_setopt($curl, CURLOPT_UNRESTICTED_AUTH, TRUE);
curl_setopt($curl, CURLOPT_SSLVERSION,3);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_POST, 0);
//curl_setopt($curl, CURLOPT_POST, true);
//curl_setopt($curl, CURLOPT_POSTFIELDS, $params );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($curl, CURLOPT_URL, $submit_url);


// Setting curl options
//curl_setopt_array( $ch, $options ); 
$result = curl_exec($curl);
curl_close($curl);
echo $result;
/****************end getting data********************/
/****************start getting data********************
$ch = curl_init("http://www.mathinstitutes.org/math-videos/metadata/v1/asset/".$assetID);    
//$ch = curl_init("http://www.mathinstitutes.org/math-videos/metadata/v1/asset/"); // catalog
$options = array(
  CURLOPT_POST =>0,
  CURLOPT_RETURNTRANSFER => true,
 CURLOPT_HEADER => true,
 CURLOPT_USERPWD=>"$username:$password");

// Setting curl options
curl_setopt_array( $ch, $options ); 
$result = curl_exec($ch);
curl_close($ch);
echo $result;
/****************end getting data********************/
/****************start deleting data********************
$assetID=208;
$ch = curl_init("http://www.mathinstitutes.org/math-videos/metadata/v1/asset/".$assetID); 
$options = array(
 CURLOPT_POST =>0,
 CURLOPT_CUSTOMREQUEST =>"DELETE",
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_HEADER => true,
 CURLOPT_USERPWD=>"$username:$password");

// Setting curl options
curl_setopt_array( $ch, $options ); 
$result = curl_exec($ch);
curl_close($ch);
echo $result;
/****************end deleting data********************/
/****************start updating , insert,data********************
$link='http://huarenads.com/images/listings/3_thumb.jpg';
$link='http://www.mathinstitutes.org/images/protein_thumb.jpg';
$ch = curl_init();
 
curl_setopt($ch, CURLOPT_POST, 0);
 
curl_setopt($ch,CURLOPT_URL,$link);
 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
$image=curl_exec($ch);
 
curl_close($ch);
$image = strtr(base64_encode(addslashes(gzcompress(serialize($image),9))), '+/=', '-_,');


$assetID=7;
$lectureDate = strtotime(date('2013-06-08 14:00:00'));
$data =  array(
				"title" => "Dooes Neuroscience Need Mathematics testing", 
			  "lectureDate"=> "$lectureDate",
				"duration"=> "600",
			  "remoteURL" => "http://huarenads.com/",
			  "location" => "Mathematics Bioscience Institute",
				"eventTitle" => "Testing", 
			  "presenters" => array( "familyName" => "Cui" ,"givenName" => "Qing",
				"title" => "Professor",
				"company" => "test company"),
				"keywords" => array("Maths","testing"),
				"mscNumbers" => array("3333","9999"),
				"timecode" => array(array("timecode"=>100,
				"timecode_type" => "Slide",
				"data" =>"Modeling in Ecology"),array("timecode"=>15,
				"timecode_type" => "Slide2",
				"data" =>"Modeling in Ecology2")),
				"rating" =>2,
				"thumbnail" => array("image" => $image, "type"=>"jpg")
			  );                                                                         
$data_string = "json=".json_encode($data); 
$ch = curl_init("http://www.mathinstitutes.org/math-videos/metadata/v1/asset/".$assetID); //update
//$ch = curl_init("http://www.mathinstitutes.org/math-videos/metadata/v1/asset/"); //insert
$options = array(
  CURLOPT_POST => 1,
  CURLOPT_RETURNTRANSFER => true,
	CURLOPT_HEADER => true,
  CURLOPT_POSTFIELDS => $data_string,
 CURLOPT_USERPWD=>"$username:$password");

// Setting curl options
curl_setopt_array( $ch, $options ); 
$result = curl_exec($ch);
curl_close($ch);
echo $result;
/****************end updating data********************/
/******************timecode*****************
$assetID=208;
$data =  array(
					"timecode" => array(array("timecode"=>200,
				"timecode_type" => "Slide",
				"data" =>"Modeling in Ecology"),array("timecode"=>305,
				"timecode_type" => "Slide2",
				"data" =>"Modeling in Ecology2"))
			  );                                                                         
$data_string = "json=".json_encode($data); 

$ch = curl_init("http://www.mathinstitutes.org/math-videos/metadata/v1/asset/".$assetID."/timecode"); 
$options = array(
  CURLOPT_POST => 1,
	
  CURLOPT_RETURNTRANSFER => true,
	CURLOPT_HEADER => true,
  CURLOPT_POSTFIELDS => $data_string,
 CURLOPT_USERPWD=>"$username:$password");

// Setting curl options
curl_setopt_array( $ch, $options ); 
$result = curl_exec($ch);
curl_close($ch);
echo $result;
/******************end*****************/
/******************statistics*****************
$assetID=22;
$data =  array(
				"rating" =>"2",
				"views" =>"234"
			  );                                                                              
$data_string = "json=".json_encode($data); 
$ch = curl_init("http://www.mathinstitutes.org/math-videos/metadata/v1/asset/".$assetID."/statistics");  

//delete  need add CURLOPT_CUSTOMREQUEST=>'DELETE', to options
$options = array(
  CURLOPT_POST => 1, 
  CURLOPT_RETURNTRANSFER => true,
	CURLOPT_HEADER => true,
  CURLOPT_POSTFIELDS => $data_string,
 CURLOPT_USERPWD=>"$username:$password");

// Setting curl options
curl_setopt_array( $ch, $options ); 
$result = curl_exec($ch);
curl_close($ch);
echo $result;
/******************end*****************/
/******************thumbnail*****************
$link='http://huarenads.com/images/listings/3_thumb.jpg';
$link='http://www.mathinstitutes.org/images/protein_thumb.jpg';
$ch = curl_init();
 
curl_setopt($ch, CURLOPT_POST, 0);
 
curl_setopt($ch,CURLOPT_URL,$link);
 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
$image=curl_exec($ch);
 
curl_close($ch);
$image = strtr(base64_encode(addslashes(gzcompress(serialize($image),9))), '+/=', '-_,');
$assetID=22;
$data =  array(
				"thumbnail" => array("image" => $image, "type"=>"jpg")
			  );                                                                         
$data_string = "json=".json_encode($data); 
$ch = curl_init("http://www.mathinstitutes.org/math-videos/metadata/v1/asset/".$assetID."/thumbnail"); 
$options = array(
  CURLOPT_POST => 1,
  CURLOPT_RETURNTRANSFER => true,
	CURLOPT_HEADER => true,
  CURLOPT_POSTFIELDS => $data_string,
 CURLOPT_USERPWD=>"$username:$password");

// Setting curl options
curl_setopt_array( $ch, $options ); 
$result = curl_exec($ch);
curl_close($ch);
echo $result;
/******************end*****************/
/******************catelog*****************

//echo "http://www.mathinstitutes.org/math-videos/metadata/v1/post/asset/".$assetID."/timecode<br>";
$ch = curl_init("http://www.mathinstitutes.org/math-videos/metadata/v1/asset/catalog"); 
$options = array(
  CURLOPT_RETURNTRANSFER => true,
	CURLOPT_HEADER => false,
 CURLOPT_USERPWD=>"$username:$password");

// Setting curl options
curl_setopt_array( $ch, $options ); 
$result = curl_exec($ch);
//$obj=json_decode($result);
curl_close($ch);
echo $result;

//print_r($obj);
//echo "<br>";
//echo $obj["assetid"];

/******************end*****************/
db_close($conn);
?>